﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.Office.Server.Search.Query;
using SharePointSearchBench.Library;

namespace SharePointSearchBench.Tests
{
    public class TestSearchBenchContext : ISearchBenchContext
    {
        Search search;

        string queryPacket;

        public TestSearchBenchContext(Search search)
        {
            this.search = search;
        }

        public bool RequiresCredentials { get { return false; } }

        public void PrepareSearch()
        {
            this.queryPacket = string.Empty;

            if(search.ApiSource == ApiSource.Service)
            {
                this.PrepareServiceSearch();
            }
            else
            {
                if(search.SearchType == SearchType.FullText)
                {
                    this.PrepareFullTextSqlSearch();
                }
                else
                {
                    this.PrepareKeywordSearch();
                }
            }
        }

        public DataSet Execute()
        {
            DataSet results = new DataSet();
            DataTable table = this.GetDataTableFromViewFields(); 
            results.Tables.Add(table);
            return results;
        }

        void PrepareFullTextSqlSearch()
        {
            //do nothing
        }

        void PrepareKeywordSearch()
        {
            //do nothing
        }

        void PrepareServiceSearch()
        {
            if(this.search.SearchType == SearchType.Keyword)
            {
                this.queryPacket = this.search.BuildKeywordQueryPacket();
            }
            else
            {
                this.queryPacket = this.search.BuildFullTextQueryPacket();
            }

            this.search.FinalQueryXml = this.queryPacket;
        }

       
        DataTable GetDataTableFromViewFields()
        {
            DataTable table = new DataTable();
            foreach(string field in this.search.DisplayFields)
            {
                table.Columns.Add(new DataColumn(field));
            }
            return table;
        }

        public Collection<SearchProperty> GetSearchProperties()
        {
            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("Name", typeof(string)));
            table.Columns.Add(new DataColumn("Description", typeof(string)));
            table.Columns.Add(new DataColumn("Type", typeof(string)));
            table.Columns.Add(new DataColumn("Retrievable", typeof(bool)));
            table.Columns.Add(new DataColumn("FullTextQueriable", typeof(bool)));


            for (int i = 0; i < 10; i++)
            {
                DataRow row = this.CreateNewSearchPropertyDataRow(table);
                table.Rows.Add(row);
            }

            DataSet dataSet = new DataSet();
            dataSet.Tables.Add(table);

            Collection<SearchProperty> properties = SearchProperty.GetPropertiesFromRawDataSet(dataSet);

            return properties;
        }





        public Collection<SearchScope> GetScopes()
        {
            DataSet dataSet = new DataSet();
            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("Name", typeof(string)));

            for(int i = 0;i < 10;i++)
            {
                DataRow row = this.CreateNewScopeDataRow(table);
                table.Rows.Add(row);
            }
            dataSet.Tables.Add(new DataTable());
            dataSet.Tables.Add(table);
            Collection<SearchScope> scopes = SearchScope.GetScopesFromRawDataSet(dataSet);
            return scopes;
        }

        DataRow CreateNewSearchPropertyDataRow(DataTable table)
        {
            DataRow row;
            row = table.NewRow();
            row["Name"] = Guid.NewGuid().ToString();
            row["Description"] = Guid.NewGuid().ToString();
            row["Type"] = Guid.NewGuid().ToString();
            row["Retrievable"] = true;
            row["FullTextQueriable"] = true;
            return row;
        }

        DataRow CreateNewScopeDataRow(DataTable table)
        {
            DataRow row;
            row = table.NewRow();
            row["Name"] = Guid.NewGuid().ToString();
            return row;
        }


    }
}
